"""
自定义组件
"""
from block import BlockChainOperation
from django.utils.safestring import mark_safe

class Pagination(object):

    def __init__(self,request,form,count,page_size = 10,page_param="page"):
        page = request.GET.get(page_param)
        if page:
            page = int(page)
        else:
            page=1
        self.page = page

        # 计算总页码
        self.count = count
        total_page,div = divmod(self.count,page_size)
        total_page = total_page+1

        plus = 5
        if total_page <= plus*2+1:
            #数据库的数据比较少
            start_page = 1
            end_page = total_page
        else:
            #数据库的数据比较多
            if page <= plus:
                start_page = 1
                end_page = 2*plus+1
            else:
                start_page = page - plus
                end_page = page + plus+1
        start = (self.page-1)*10
        end = self.page*10+1
        self.form = form[start:end]

        #增加get列表
        import copy
        get_object = copy.deepcopy(request.GET)
        get_object._mutable = True
        # 页码
        page_str_list = []
        for i in range(start_page, end_page+1):
            get_object.setlist('page', [i])
            self.get_object = get_object
            if i == page:
                ele = '<li class = "active"><a href="?{}">{}</a></li>'.format(self.get_object.urlencode(), i)
            else:
                ele = '<li><a href="?{}">{}</a></li>'.format(self.get_object.urlencode(), i)
            page_str_list.append(ele)

        # 将其转换为html
        self.page_string = mark_safe("".join(page_str_list))